home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dutil / makeproto.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  4KB  |  206 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  MAKEPROTO.C
  9.  *
  10.  *  MAKEPROTO [-o outfile] [-f field] file1 file2 ... fileN
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #ifdef AMIGA
  18. #include <exec/types.h>
  19. #include <exec/lists.h>
  20. #include <clib/exec_protos.h>
  21. #include <clib/alib_protos.h>
  22. #include <lib/version.h>
  23. #else
  24. #include <suplib/lists.h>
  25. #include <include/lib/version.h>
  26. #endif
  27.  
  28. typedef struct List List;
  29. typedef struct Node Node;
  30.  
  31. void ScanFile(char *);
  32. void help(int);
  33. void xprintf(const char *, ...);
  34. void DumpNodeList(void);
  35. int EndsWithSlash(const char *);
  36.  
  37. #ifdef AMIGA
  38. #ifdef _DCC
  39. IDENT("makeproto",".4");
  40. DCOPYRIGHT;
  41. #endif
  42. #endif
  43.  
  44. char    *Field = "Prototype";
  45. char    *OutFile;
  46. int    FieldLen = 9;
  47. int    _DiceCacheEnable = 1;
  48. List    OutList;
  49.  
  50. int
  51. main(int ac, char **av)
  52. {
  53.     short i;
  54.  
  55.     NewList(&OutList);
  56.  
  57.     for (i = 1; i < ac; ++i) {
  58.     char *ptr = av[i];
  59.     if (*ptr != '-') {
  60.         ScanFile(ptr);
  61.         continue;
  62.     }
  63.     ptr += 2;
  64.     switch(ptr[-1]) {
  65.     case 'f':
  66.         if (*ptr == 0)
  67.         ptr = av[++i];
  68.         Field = ptr;
  69.         FieldLen = strlen(ptr);
  70.         break;
  71.     case 'o':
  72.         if (*ptr == 0)
  73.         ptr = av[++i];
  74.         OutFile = ptr;
  75.         xprintf("\n/* MACHINE GENERATED */\n\n");
  76.         break;
  77.     default:
  78.         printf("illegal option: -%c\n", ptr[-1]);
  79.         help(1);
  80.     }
  81.     }
  82.     if (ac == 1)
  83.     help(0);
  84.     else
  85.     DumpNodeList();
  86.     return(0);
  87. }
  88.  
  89. void
  90. help(int code)
  91. {
  92.     fputs("MAKEPROTO - Scans for 'Prototype' lines in source files\n", stderr);
  93.     fputs("makeproto [-o outfile] file1 file2... fileN\n", stderr);
  94.     exit(code);
  95. }
  96.  
  97. void
  98. ScanFile(file)
  99. char *file;
  100. {
  101.     FILE *fi = fopen(file, "r");
  102.     char buf[512];
  103.  
  104.     if (fi == NULL) {
  105.     fprintf(stderr, "makeproto: couldn't open %s\n", file);
  106.     return;
  107.     }
  108.     xprintf("\n/* %-20s */\n\n", file);
  109.     while (fgets(buf, sizeof(buf), fi)) {
  110.     char *ptr = buf;
  111.  
  112.     if (*ptr == ';')
  113.         ++ptr;
  114.     if (strncmp(ptr, Field, FieldLen) == 0) {
  115.         xprintf("%s", ptr);
  116.         while (EndsWithSlash(ptr)) {
  117.         if (fgets(buf, sizeof(buf), fi) == NULL)
  118.             break;
  119.         if (*ptr == ';')
  120.             ++ptr;
  121.         xprintf("%s", ptr);
  122.         }
  123.     }
  124.     }
  125.     fclose(fi);
  126. }
  127.  
  128. /*
  129.  *  xprintf() - write to a node list
  130.  */
  131.  
  132. void
  133. xprintf(const char *ctl, ...)
  134. {
  135.     va_list va;
  136.     static unsigned char Buf[256];
  137.     short i;
  138.     Node *node;
  139.  
  140.     va_start(va, ctl);
  141.     i = vsprintf(Buf, ctl, va);
  142.     va_end(va);
  143.  
  144.     node = malloc(sizeof(Node) + i + 1);
  145.     node->ln_Name = (char *)(node + 1);
  146.     strcpy(node->ln_Name, Buf);
  147.     AddTail(&OutList, node);
  148. }
  149.  
  150. void
  151. DumpNodeList(void)
  152. {
  153.     FILE *fo;
  154.     Node *node;
  155.     short i;
  156.  
  157.     if (OutFile && (fo = fopen(OutFile, "r"))) {
  158.     for (node = OutList.lh_Head; node->ln_Succ; node = node->ln_Succ) {
  159.         for (i = 0; node->ln_Name[i] && (unsigned char)node->ln_Name[i] == getc(fo); ++i)
  160.         ;
  161.         if (node->ln_Name[i])
  162.         break;
  163.     }
  164.  
  165.     /*
  166.      *  no change to file, do not update (this allows us to have a
  167.      *  DMakefile dependancy on the prototype file to force, for
  168.      *  example, precompiled defs to be recreated)
  169.      */
  170.  
  171.     if (node->ln_Succ == NULL && getc(fo) == EOF) {
  172.         fclose(fo);
  173.         return;
  174.     }
  175.     fclose(fo);
  176.     }
  177.     if (OutFile)
  178.     fo = fopen(OutFile, "w");
  179.     else
  180.     fo = stdout;
  181.  
  182.     if (fo) {
  183.     while ((node = RemHead(&OutList)) != NULL)
  184.         fwrite(node->ln_Name, 1, strlen(node->ln_Name), fo);
  185.     } else {
  186.     fprintf(stderr, "Unable to create %s\n", OutFile);
  187.     exit(20);
  188.     }
  189.  
  190.     if (fo && OutFile)
  191.     fclose(fo);
  192. }
  193.  
  194. int
  195. EndsWithSlash(const char *ptr)
  196. {
  197.     if ((ptr = strrchr(ptr, '\\')) != NULL) {
  198.     for (++ptr; *ptr == ' ' || *ptr == '\t'; ++ptr)
  199.         ;
  200.     if (*ptr == 0 || *ptr == '\n')
  201.         return(1);
  202.     }
  203.     return(0);
  204. }
  205.  
  206.